package com.zhijian.wharf.service.impl;

import com.zhijian.wharf.dao.AisPositionMapper;
import com.zhijian.wharf.entity.po.AisPosition;
import com.zhijian.wharf.entity.vo.request.AisPositionSearchVo;
import com.zhijian.wharf.entity.vo.request.AisPositionSaveOrUpdateVo;
import com.zhijian.wharf.entity.vo.response.AisPositionVo;
import com.zhijian.wharf.service.AisPositionService;
import com.zhijian.util.page.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.util.AssertUtil;
import org.springframework.beans.BeanUtils;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * AIS船舶定位业务处理层
 * 
 * @author auto
 * @version: 1.0
 * @date 2024-08-18 12:47:07
 */
@Slf4j
@Service
public class AisPositionServiceImpl implements AisPositionService {

	@Autowired
    private AisPositionMapper mapper;

    @Override
    public AisPosition selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public AisPositionVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByIdList(List<Long> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            return;
        }
        mapper.deleteByIdList(idList);
    }

    @Override
    public Long insert(AisPosition aisPosition) {
        Date now = new Date();
        aisPosition.setCreateTime(now);
        aisPosition.setUpdateTime(now);
        return mapper.insert(aisPosition) > 0 ? aisPosition.getId() : 0L;
    }

    @Override
    public Long insertSelective(AisPosition aisPosition) {
        Date now = new Date();
        aisPosition.setCreateTime(now);
        aisPosition.setUpdateTime(now);
        return mapper.insertSelective(aisPosition) > 0 ? aisPosition.getId() : 0L;
    }

    @Override
    public boolean update(AisPosition aisPosition) {
        Date now = new Date();
        aisPosition.setUpdateTime(now);
        return mapper.updateByPrimaryKey(aisPosition) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(AisPosition record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public boolean save(AisPositionSaveOrUpdateVo saveOrUpdateVo) {
        AisPosition aisPosition = new AisPosition();
        BeanUtils.copyProperties(saveOrUpdateVo, aisPosition);
        return this.insert(aisPosition) != 0;
    }

    @Override
    public boolean modify(AisPositionSaveOrUpdateVo saveOrUpdateVo) {
        AisPosition old = this.selectById(saveOrUpdateVo.getId());
        AssertUtil.notNull(old, "此数据不存在或者已被删除");
        AisPosition aisPosition = new AisPosition();
        BeanUtils.copyProperties(saveOrUpdateVo, aisPosition);
        return this.update(aisPosition);
    }

    @Override
    public AisPositionVo selectOne(AisPositionSearchVo aisPositionSearchVo) {
        return mapper.selectOne(aisPositionSearchVo);
     }

    @Override
    public Integer count(AisPositionSearchVo aisPositionSearchVo) {
        return mapper.count(aisPositionSearchVo);
    }

    @Override
    public List<AisPositionVo> selectList(AisPositionSearchVo aisPositionSearchVo) {
        return mapper.selectList(aisPositionSearchVo);
    }

    @Override
    public Page<AisPositionVo> selectPage(AisPositionSearchVo aisPositionSearchVo) {
        AssertUtil.check(Objects.isNull(aisPositionSearchVo.getPageNum()) || Objects.isNull(aisPositionSearchVo.getPageSize()), "分页参数不能为空");
        //先直接查总数
        int count = count(aisPositionSearchVo);
        //分页
        Page<AisPositionVo> page = new Page<>(true, count, aisPositionSearchVo.getPageNum(), aisPositionSearchVo.getPageSize());
        aisPositionSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<AisPositionVo> responseVos = this.selectList(aisPositionSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<AisPosition> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    @Override
    public void deleteByCondition(AisPositionSearchVo aisPositionSearchVo) {
        //慎重用此方法！！
        mapper.deleteByCondition(aisPositionSearchVo);
    }
    
}